//
//  RecommendVC.m
//  ZZLC
//
//  Created by 杨春至 on 2017/12/13.
//  Copyright © 2017年 ZhongZeng. All rights reserved.
//

#import "RecommendVC.h"
#import "RecommendTableViewCell.h"
#import <SDCycleScrollView.h>
#import "MMMDetailVC.h"
#import "MMExtraVC.h"
#import "ZZWebViewController.h"
#import "MJDIYHeader.h"

static NSString *const IDE  = @"recommend_cell_ide";
@interface RecommendVC ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate,
DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
{
    NSMutableArray *_bannerArray;
}
@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *dataSource;

@property (nonatomic,strong) SDCycleScrollView *cycleBannerView;

@end

@implementation RecommendVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
//    [self loadData];
    
}
- (void)loadData{
//    self.dataSource = @[@"",@"",@""].mutableCopy;
    [NetWorkingManager postWithRequestUrl:@"indexRecommend" AndParameters:@{@"parameters":[@{@"lastTime":@""} DataTOjsonString]} AndRequestBlock:^(NSDictionary *responseObject) {
        [self configUIByData:responseObject];
        [_tableView.mj_header endRefreshing];
    } FailBlock:^(NSDictionary *errDic) {
        [_tableView.mj_header endRefreshing];
    }];
}
- (void)configUI{
    [self.view addSubview:self.tableView];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}
- (void)configUIByData:(NSDictionary *)responseObject{
    if ([responseObject.allKeys containsObject:@"bannerList"]) {
        _bannerArray = [responseObject objectForKey:@"bannerList"];
        NSMutableArray  *imageArray = [NSMutableArray new];
        for (NSDictionary *dict in _bannerArray) {
            [imageArray addObject:dict[@"imgURL"]];
        }
        self.cycleBannerView.imageURLStringsGroup = imageArray;
    }
    if ([responseObject.allKeys containsObject:@"list"]) {
        self.dataSource = [ProduceModel objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]];
//        [self.tableView reloadData];
        [self reloadDataByNowState];
    }
}
#pragma mark Delegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    if (_bannerArray.count+1>=index) {
        NSString *url  = [_bannerArray[index]objectForKey:@"noticeUrl"];
        ZZWebViewController *webVC = [[ZZWebViewController alloc]init];
        webVC.url = url;
        [self.navigationController pushViewController:webVC animated:YES];
    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 170;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 428.0f/2;
    }else{
        return 10;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return  self.cycleBannerView;
    }else{
        return nil;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:IDE forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.section];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MMMDetailVC *dVC = [[MMMDetailVC alloc]init];
    ProduceModel *model = self.dataSource[indexPath.section];
    dVC.pId = model.productId;
    [self.navigationController pushViewController:dVC animated:YES];
}
- (void)viewDidLayoutSubviews{
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"暂无数据";
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0f],
                                 NSForegroundColorAttributeName: [UIColor darkGrayColor]};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"请确保您的网络状况是良好的";
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],
                                 NSForegroundColorAttributeName: [UIColor lightGrayColor],
                                 NSParagraphStyleAttributeName: paragraph};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:14.0f]};
    
    return [[NSAttributedString alloc] initWithString:@"~点击此处重试" attributes:attributes];
}
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
    [self loadData];
}

#pragma mark #####LAZY LOAD
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = RGBCOLOR(245, 245, 245);
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        _tableView.mj_header = [MJDIYHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
        [_tableView registerClass:[RecommendTableViewCell class] forCellReuseIdentifier:IDE];
        _tableView.separatorColor = [UIColor clearColor];
    }
    return _tableView;
}
- (SDCycleScrollView *)cycleBannerView{
    if (!_cycleBannerView) {
        // 网络加载图片的轮播器
//        /750*KWidth
        _cycleBannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KWidth, 428/2) delegate:self placeholderImage:UIImageMake(@"banner")];
    }
    return _cycleBannerView;
}

-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.navigationController.navigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self loadData];
}
- (void)reloadDataByNowState{
    if ([UserModel isLogin]) {
        BOOL buyFreshmanProduct = [[UserModel userDic][@"buyFreshmanProduct"] boolValue];
        if (buyFreshmanProduct) {
            NSMutableArray *newArray = [NSMutableArray new];
            for (ProduceModel *model in self.dataSource) {
                if ([model.type isEqualToString: @"1"]) {
                    
                }else{
                    [newArray addObject:model];
                }
            }
            self.dataSource = [newArray mutableCopy];        
        }
    }
    
    [self.tableView reloadData];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWidth, 64)];
    view.backgroundColor = RGBCOLOR(63, 66, 71);
    [self.navigationController.navigationBar setBackgroundImage:[view Getimage] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBarHidden = NO;
}
@end
